package com.dongdongshop.operator.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.operator.pojo.TbBrand;
import com.dongdongshop.operator.pojo.TbSpecification;
import com.dongdongshop.operator.pojo.TbTypeTemplate;
import com.dongdongshop.operator.service.SpecificationService;
import com.dongdongshop.operator.service.TbBrandService;
import com.dongdongshop.operator.service.TypeTemplateService;
import com.dongdongshop_util.Result;
import com.dongdongshop_util.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("typeTemplate")
public class TypeTemplateController {

    private final Logger logger = LoggerFactory.getLogger(SpecificationController.class);

    @Reference(check = false)
    private TypeTemplateService typeTemplateService;
    @Reference(check = false)
    private TbBrandService brandService;
    @Reference(check = false)
    private SpecificationService specificationService;

    @RequestMapping("toTypeTemplate")
    public String toTypeTemplate(Model model){
        //查询品牌管理表
        List<TbBrand> brandList = brandService.getBrand();
        model.addAttribute("brandList",brandList);
        //查询规格管理表
        List<TbSpecification> specificationList = specificationService.getSpecification(null);
        model.addAttribute("specificationList",specificationList);
        return "admin/type_template";
    }

    //获取模块管理
    @ResponseBody
    @RequestMapping("getTypeTemplate")
    public Result getTypeTemplate(String name){
        try {
            List<TbTypeTemplate> typeTemplateList = typeTemplateService.getTypeTemplate(name);
            return Result.success(ResultEnum.SUCCESS,typeTemplateList);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //增加操作
    @ResponseBody
    @RequestMapping("addTypeTemplate")
    public Result addTypeTemplate(TbTypeTemplate typeTemplate){
        try {
            typeTemplateService.addTypeTemplate(typeTemplate);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //回显操作
    @ResponseBody
    @RequestMapping("toUpdate")
    public Result toUpdate(Long id){
        try {
            TbTypeTemplate typeTemplate = typeTemplateService.getTypeTemplateById(id);
            return Result.success(ResultEnum.SUCCESS,typeTemplate);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //修改操作
    @ResponseBody
    @RequestMapping("updateTypeTemplate")
    public Result updateTypeTemplate(TbTypeTemplate typeTemplate){
        try {
            typeTemplateService.updateTypeTemplate(typeTemplate);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //批量删除
    @ResponseBody
    @RequestMapping("deleteTypeTemplateByIds")
    public Result deleteTypeTemplateByIds(@RequestParam(value = "ids[]")Long[] ids){
        try {
            typeTemplateService.deleteTypeTemplateByIds(ids);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
}
